﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Packaging;

namespace DataPacker
{
    class Program
    {
        static void Main(string[] args)
        {
            Switch type = Switch.Undefined;
            PackagePart packagePartDocument = null;
            List<string> input = new List<string>();
            string output = ".", relative;
            string[] files;
            bool removeDirs = false, backgroundMode = false;

            foreach (string arg in args)
            {
                if (arg.Contains('-'))
                    switch (arg.Remove(0, 1).ToLower())
                    {
                        case "r": removeDirs = true; break;
                        case "b": backgroundMode = true; break;
                        case "i": type = Switch.Input; break;
                        case "o": type = Switch.Output; break;
                        default: type = Switch.Undefined; break;
                    }
                else
                {
                    switch (type)
                    {
                        case Switch.Input:
                            input.Add(arg.Replace("/", "\\"));
                            break;

                        case Switch.Output:
                            output = arg.Replace("/", "\\");
                            break;

                        default:
                            break;
                    }
                }
            }

            if (!Directory.Exists(output))
            {
                Console.Out.WriteLine("Creating output dir");
                Directory.CreateDirectory(output);
            }

            foreach (string dir in input)
            {
                string name = dir.Substring(dir.LastIndexOf("\\") + 1);

                Console.Out.WriteLine("Processing Directory: " + name);

                using (ZipPackage package = (ZipPackage)ZipPackage.Open(Path.Combine(output, name) + ".xep", FileMode.Create, FileAccess.ReadWrite))
                {
                    Console.Out.WriteLine("* ZipPackage started");

                    try
                    {
                        files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
                    }
                    catch (Exception e)
                    {
                        Console.Out.WriteLine("* !! " + e.Message);
                        continue;
                    }

                    foreach (string file in files)
                    {
                        relative = file.Remove(0, dir.Length + 1).Replace("\\", "/");

                        Console.Out.WriteLine("** Processing file: " + relative);

                        packagePartDocument = package.CreatePart(new Uri("/" + relative, UriKind.Relative), System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Maximum);

                        using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            Console.Out.WriteLine("*** Processing file data stream: " + file);
                            fileStream.CopyTo(packagePartDocument.GetStream());
                        }
                    }

                    Console.Out.WriteLine("* ZipPackage finished!");
                }

                if (removeDirs)
                {
                    Console.Out.WriteLine("Deleting input directory");
                    Directory.Delete(dir, true);
                }
            }

            if (!backgroundMode)
            {
                Console.Out.WriteLine();
                Console.Out.Write("Press any key to exit...");
                Console.ReadKey();
            }
        }

        private enum Switch
        {
            Undefined,
            Input,
            Output
        }
    }
}
